const Model = require('./model')
const BaseMongo = require('../base')
const { stateKeys } = require('../base/model')

/**
 * 个人数据
 */
class MeMongo extends BaseMongo {
	constructor(){
		super()
	}

	// ============================== 基本数据 ==============================

	static getMe() {
		return super.findOne()
	}

	/**
	 * 更新数据
	 * @param data 需要更新的数据
	 * @returns {Promise<*|IDBValidKey|void|IDBValidKey>}
	 */
	static update(data){
		return Model.updateOne(
			{},
			this.getJson(data, ['home_icon', 'home_title', 'home_jianli',
				'abouts', 'expertise', 'tiosNum', 'nodeNum', 'twebNum',
				'xcxNum', 'javaNum', 'iosShow', 'webShow', 'nodeShow', 'xcxShow', 'javaShow', 'experience_show',
			]),
			{multi: true})
	}

	// ============================== 工作 ==============================
	/**
	 * 添加工作经历
	 * @param data 需要添加的数据
	 * @returns {Promise<*|IDBValidKey|void|IDBValidKey>}
	 */
	static addWork(data){
		return Model.updateOne({}, {
			$push: {
				works: this.getJson(data, ['startTime', 'endTime', 'name', 'icon', 'detail', 'obligation', 'url'])
			}
		})
	}

	/**
	 * 获取所有工作经历
	 * @returns {Promise<*>}
	 */
	static async getWorks(){
		const meModel = await super.findOne({}, {
			works: 1
		})
		return meModel.works.filter((item) => {
			return item.stateKey === stateKeys.NORMAL
		})
	}

	/**
	 * 删除工作经历
	 * @param id 要删除的ID
	 * @param thorough 是否彻底的删除数据
	 * @returns {Promise<*|IDBValidKey|void|IDBValidKey>}
	 */
	static removeWork(id, thorough = false){
		if (thorough) {
			return Model.updateOne({}, {
				$pull: {
					works: {
						_id: this.objId(id)
					}
				}
			})
		} else {
			return this.modifyWorkState(id, stateKeys.DELETE)
		}
	}

	/**
	 * 更新工作数据
	 * @param data 更新的数据
	 * @returns {Promise<*|IDBValidKey|void|IDBValidKey>}
	 */
	static updateWork(data){
		return Model.updateOne(
			{
				'works._id': this.objId(data.id)
			},
			{
				$set: {
					'works.$': this.getJson(data, ['startTime', 'endTime', 'name', 'icon', 'detail', 'obligation', 'url'])
				}
			})
	}

	/**
	 * 获取所有工作经历
	 * @returns {Promise<*>}
	 */
	static async findWorkDetail(id){
		const meData = await super.findOne({}, {
			works: 1
		})
		return meData.works.id(id)
	}

	/**
	 * 修改工作数据状态
	 * @param id 数据的唯一ID
	 * @param state 数据的状态
	 * @returns {Promise<*>}
	 */
	static async modifyWorkState(id, state) {
		const workModel = await this.findWorkDetail(id)
		workModel.stateKey = state
		delete workModel._id
		return Model.updateOne(
			{
				'works._id': this.objId(id)
			},
			{
				$set: {
					'works.$': workModel
				}
			})
	}

	/**
	 * 添加到回收站
	 * @param id 数据ID
	 * @returns {*}
	 */
	static work_dustbin_push(id) {
		return this.modifyWorkState(id, stateKeys.DUSTBIN)
	}

	/**
	 * 从回收站还原
	 * @param id
	 * @returns {*}
	 */
	static work_dustbin_pop(id) {
		return this.modifyWorkState(id, stateKeys.NORMAL)
	}

	/**
	 * 获取所回收站数据
	 * @returns {Promise<void>}
	 */
	static async work_findDustbin() {
		const meModel = await super.findOne({}, {
			works: 1
		})
		return meModel.works.filter((item) => {
			return item.stateKey === stateKeys.DUSTBIN
		})
	}

	/**
	 * 获取工作个数
	 * @returns {Promise<void>}
	 */
	static async work_count() {
		const meModel = await super.findOne({}, {
			works: 1
		})
		let normalCount = 0
		let dustbinCount = 0
		let deleteCount = 0
		let allCount = 0
		if (meModel && meModel.works.length > 0) {
			meModel.works.forEach((item) => {
				switch (item.stateKey) {
					case stateKeys.DELETE:
						deleteCount += 1
						break
					case stateKeys.DUSTBIN:
						dustbinCount += 1
						break
					default:
						normalCount += 1
						break
				}
			})
		}
		return {
			allCount,
			normalCount,
			dustbinCount,
			deleteCount
		}
	}
}

/**
 * 设置当前模型
 */
MeMongo.model = Model

module.exports = MeMongo
